package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.LoginInfor;
import com.itheima.pojo.PageQuery;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.User;
import com.itheima.service.Userservice;
import com.itheima.utils.TokenMaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements Userservice {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult page(PageQuery pageQuery) {
        Integer page = pageQuery.getPage();
        Integer pageSize = pageQuery.getPageSize();

        Integer total = userMapper.findTotal(pageQuery);

        Integer start = (page-1)*pageSize;
        pageQuery.setPage(start);
        return new PageResult(total,userMapper.page(pageQuery));
    }

    public LoginInfor login(User user) {
        User user1 = userMapper.login(user);
        if(user1 != null){
            //为角色创建令牌
            Map<String,Object> map = new HashMap<>();
            map.put("id",user1.getId());
            map.put("name",user1.getName());
            String token = TokenMaker.makeToken(map);
            LoginInfor loginInfor = new LoginInfor(user,token);
            return loginInfor;
        }
        return null;
    }
}
